﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ViewBranches : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvBranches.PageIndex = e.NewPageIndex;
        gvBranches.DataBind();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        //Since we are changing the parameters we will not use the cached

        //object datasource we will call the selecte method again.

        if (ObjectDataSourceBranches.SelectParameters[0].DefaultValue == null ||
            ObjectDataSourceBranches.SelectParameters[1].DefaultValue == null ||
            ObjectDataSourceBranches.SelectParameters[0].DefaultValue !=
            e.SortExpression ||
            ObjectDataSourceBranches.SelectParameters[1].DefaultValue == "Desc")
        { //sort direction

            ObjectDataSourceBranches.SelectParameters[1].DefaultValue = "Asce";
        }
        else if (ObjectDataSourceBranches.SelectParameters[1].DefaultValue == "Asce")
        {
            ObjectDataSourceBranches.SelectParameters[1].DefaultValue = "Desc";

        }

        //Which column to sort on.

        ObjectDataSourceBranches.SelectParameters[0].DefaultValue = e.SortExpression;

        //We have to do this or we will get an exception

        e.Cancel = true;
    }
    protected void btnAddBranch_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Pages_Admin/ManageBranches/AddBranch.aspx");
    }
    protected void btnDeleteBranch_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow row in gvBranches.Rows)
            {
               CheckBox ck=(row.FindControl("chkselect") as CheckBox);
                if (ck.Checked)
                {
                    string branchID = ck.ToolTip;
                    DALBranch.DeleteBranch(branchID);
                }
                else continue;
            }
        }
        catch 
        {
            lblMessage.Text = "Delete Fail : There was an error, the operation was terminated";
        }
        gvBranches.DataBind();

       // Response.Redirect("~/Pages_Admin/ManageBranches/ViewBranches.aspx");
    }
}
